﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////


#pragma once
#include "GnufExtension.h"
#include "GnufItemEvents.h"
#include "GnufPallet.h"
#include "GnufNameValuePairs.h"

namespace gnuf
{
    class Toolbar;
    class NameValuePairs;

    enum class FormAction
    {
        Submit,
        Cancel
    };

    class GNUF_EXTENSION_EXPORT GnufFormPallet final
        : public IPalletEventHandler
        , public IItemEventHandler
    {
        friend class QtBnufFormPallet;
    public:
        typedef typename std::map<std::wstring, std::wstring> FormSubmitDatas;
        typedef typename std::function<bool()> ValidCheckAction;
    public:
        GnufFormPallet();
        virtual ~GnufFormPallet() {}

    public:
        void setCaption(const std::wstring&);
        std::wstring caption() const;

        void setSubmitActionName(const std::wstring&);
        std::wstring submitActionName() const;

        void setHeight(int height);
        int height() const;

        void setWidth(int width);
        int width() const;

        void setItemError(const std::wstring& itemName, const std::wstring& errorText);

        void addLineEditFormItem(
            const std::wstring& itemName,
            const std::wstring& defaultText = L"",
            const std::wstring& description = L"",
            const std::wstring& validator = L""
        );

        void addComboBoxFormItem(
            const std::wstring& itemName,
            const std::list<std::wstring>& opitions,
            int defaultOptionIndex = 0,
            const std::wstring& description = L""
        );

        void setValidCheckAction(const ValidCheckAction& action) { m_validCheckAction = action; }

        void clearFormContents();

        FormSubmitDatas submitDatas() const { return m_submitDatas; }
        FormAction exec();
        FormAction action() { return m_action; }

    protected:
        virtual void onHeaderButtonClicked(Pallet *) override;
        virtual bool onFooterButtonClicked(Pallet *) override;

    private:
        void initialize();
        void reset();
        void refreshStyle();
        void readSubmitData();
        void clearError();
        Toolbar* toolBar() { return m_toolbar.get(); }

    private:
        gcmp::OwnerPtr<Pallet> m_pallet;
        gcmp::OwnerPtr<Toolbar> m_toolbar;
        gcmp::WeakPtr<NameValuePairs> m_formItems;
        FormAction m_action;
        FormSubmitDatas m_submitDatas;
        ValidCheckAction m_validCheckAction;
    };
}



